import { login } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { Message } from 'element-ui'

export default {
  namespaced: true,
  state() {
    return {
      token: getToken() || ''
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    logout(state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    async login(context, { mobile, password }) {
      const res = await login({ mobile, password })
      console.log(res)
      context.commit('setToken', res.data)
      Message.success(res.message)
    }
  }
}
